<template>
  <div class="app-container">
    <RoomTree v-model="currentTree" @change="treeClick" />
    <div class="container_body">
      <el-row :gutter="10" class="mb8">
        <el-button type="primary" icon="el-icon-refresh-left" size="mini" @click="getList">刷新</el-button>
      </el-row>

      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
        <el-row>
          <el-form-item label="房间号码" prop="roomId">
            <RoomSelect v-model="queryParams.roomId" :params="roomSelectParams" />
          </el-form-item>
          <el-form-item label="违规次数大于" prop="wgTimes">
            <el-input-number
              v-model="queryParams.wgTimes"
              style="width: 150px"
              placeholder="请输入"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>

      <el-table v-loading="loading" :data="roomList" ref="table">
        <el-table-column label="房间号码" align="center" prop="roomNo" />
        <el-table-column label="设备号" align="center" prop="meterNo" />
        <el-table-column label="违规次数" align="center" prop="wgTimes" />
        <el-table-column label="更新时间" align="center" prop="dt" width="160">
          <template slot-scope="scope">{{ formatterDt(scope.row.dt) }}</template>
        </el-table-column>
        <el-table-column label="所属楼层" align="center" prop="floorNo" />
        <el-table-column label="楼座名称" align="center" prop="buildName" />
        <el-table-column label="校区名称" align="center" prop="schoolName" />
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import RoomSelect from "@/components/RoomSelect";
import {
  htwgcList
} from "@/api/warn/backrule";
import RoomTree from "@/components/RoomTree";
import moment from 'moment';
export default {
  name: "Room",
  components: {
    RoomTree,
    RoomSelect
  },
  data() {
    return {
      zhaoceList: [],
      // 当前选中层级
      currentTree: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间档案表格数据
      roomList: [],
      roomSelectParams: {},
      getListParams: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatterDt(date) {
      return moment(date).format("yyyy-MM-DD HH:mm:ss");
    },
    /** 查询房间档案列表 */
    getList() {
      this.loading = true;
      htwgcList({
        ...this.queryParams,
        ...this.getListParams
      }).then(response => {
        this.roomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    treeClick() {
      const nameMap = {
        build: "buildId",
        floor: "floor",
        room: "roomId"
      };
      this.roomSelectParams = {
        [nameMap[this.currentTree.name]]: this.currentTree.id
      };
      const nameMap2 = {
        build: "buildId",
        floor: "floor",
        room: "roomId"
      };
      this.getListParams = {
        [nameMap2[this.currentTree.name]]: this.currentTree.id
          // this.currentTree.name == "floor"
          //   ? this.currentTree.data.name.substring(0, 1)
          //   : this.currentTree.id
      };
      this.handleQuery();
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  overflow: hidden;
  .container_body {
    box-sizing: content-box;
    flex: 1;
    max-height: 100%;
    overflow: auto;
    padding-left: 10px;
    padding-right: 5px;
  }
}
</style>
